using System.Text;
using Gateway.IQuery;
using Gateway.IQuery.Role;
using Gateway.IQuery.Role.Request;
using Gateway.IQuery.Role.Result;
using Gateway.IRepositories;
using Gateway.SqlSugar;

namespace Gateway.Query;

/// <summary>
/// 
/// </summary>
public class RoleQuery : IRoleQuery
{
    private readonly IQueryRepository _repository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    public RoleQuery(IQueryRepository repository)
    {
        _repository = repository;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<PageResult<RoleDetailResult>> GetPageList(RolePageListRequest request)
    {
        StringBuilder where = new StringBuilder(100);
        SugarParameters parameters = new SugarParameters();
        if (!string.IsNullOrEmpty(request.RoleName))
        {
            where.Append(" AND p.RoleName like @roleName");
            parameters.Add("@roleName", $"%{request.RoleName}%");
        }

        int count = 0;
        if (request.Page == 1)
        {
            count = await _repository.QueryFirstOrDefaultAsync<int>(
                $@"select count(1) from  Role p where Deleted='N' {where.ToString()}", parameters);
        }

        string sql =
            $@"select roleid, roleno, rolealias, rolename, remark, seqno, createdtime, disabled from  Role p where Deleted='N'
{where.ToString()}
order by  RoleId,CreatedTime
limit  {request.Offset},{request.PageSize}";
        var data = await _repository.QueryAsync<RoleDetailResult>(sql, parameters);
        return PageResult<RoleDetailResult>.Create(data.ToList(), count);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<RoleDetailResult> GetDetail(string id)
    {
        var data = await _repository.QueryFirstOrDefaultAsync<RoleDetailResult>(
            "select  roleid, roleno, rolealias, rolename, remark, seqno, createdby, creatorname, createdtime, lastmodifiedby, lastmodifiername, lastmodifiedtime, disabled from  Role where Deleted='N' and RoleId=@id",
            new { id });
        if (data != null)
        {
            var menus = await _repository.QueryListAsync<RoleMenuResult>(@"select * from RoleMenu where RoleId=@id",
                new { id });
            data.Menus = menus;
            var permissions = await _repository.QueryListAsync<RoleExtensionPermissionResult>(
                @"select * from RoleExtensionPermission where RoleId=@id",
                new { id });
            data.Permissions = permissions;
        }
        return data;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async Task<List<RoleSimpleResult>> GetAvailableList()
    {
        return await _repository.QueryListAsync<RoleSimpleResult>(
            "select  roleid, rolename from  Role where Deleted='N' and Disabled='N'");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task<List<RoleSimpleResult>> GetListByIds(List<string> ids)
    {
        return await _repository.QueryListAsync<RoleSimpleResult>(
            $"select  roleid, rolename from  Role where Deleted='N' and roleId in ({string.Join(",",ids.Select(p=>$"'{p}'"))}) ");
    }
}